import { INPUT_FOCUS, INPUT_BLUR, MOUSE_ENTER, MOUSE_LEAVE, CHANGE_LIST, NEXT_LIST } from './constants'
import {fromJS} from 'immutable'
import axios from 'axios'

const changeList = (data) => ({
  type:CHANGE_LIST,
  data: fromJS(data),
  pageTotal: Math.ceil(data.length / 10)
})

export const getInputFocus = () => ({
  type: INPUT_FOCUS
})
export const getInputBlur = () => ({
  type: INPUT_BLUR
})
export const getMouseEnter = () =>({
  type: MOUSE_ENTER
})
export const getMouseLeave = () => ({
  type: MOUSE_LEAVE
})
export const getNextList = (page) => ({
  type: NEXT_LIST,
  page
})

export const getListInfo = () => {
  return (dispatch) => {
    console.log('getListInfo')
    axios.get('/api/list.json').then((resp) => {
      if (resp.data.success) {
        dispatch(changeList(resp.data.data))
      }
    }).catch((e) => {
      alert('error')
      console.log(e)
    })
  }
}